package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.PageResult;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.mapper.TbItemParamMapper;
import com.usian.pojo.TbItemParam;
import com.usian.pojo.TbItemParamExample;
import com.usian.pojo.TbItemParamItem;
import com.usian.pojo.TbItemParamItemExample;
import com.usian.redis.RedisClient;
import com.usian.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Autowired
    private RedisClient redisClient;
//    ITEM_INFO: ITEM_INFO
////    BASE: BASE
////    DESC: DESC
////    PARAM: PARAM
////    ITEM_INFO_EXPIRE: 86400
    @Value("${ITEM_INFO}")
    private String ITEM_INFO;

    @Value("${PARAM}")
    private String PARAM;

    @Value("${ITEM_INFO_EXPIRE}")
    private long ITEM_INFO_EXPIRE;

    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> paramList = tbItemParamMapper.selectByExampleWithBLOBs(example);

        if (paramList.size()>0) {
            return paramList.get(0);
        }
        return null;
    }

//    查询所有商品规格模板
    @Override
    public PageResult selectItemParamAll(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemParamExample example = new TbItemParamExample();
        example.setOrderByClause("updated DESC");
//        TbItemParamExample.Criteria criteria = example.createCriteria();

        List<TbItemParam> paramList = tbItemParamMapper.selectByExampleWithBLOBs(example);
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(paramList);
        PageResult pageResult = new PageResult();
        pageResult.setResult(pageInfo.getList());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setPageIndex(pageInfo.getPageNum());

        return pageResult;
    }

//    添加商品规格参数
    @Override
    public int insertItemParam(Long itemCatId, String paramData) {
//        判断该类别的商品是否有规格模板
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> paramList = tbItemParamMapper.selectByExample(example);
        if (paramList.size()>0){
            return 0;
        }

//        保存规格模板
        TbItemParam itemParam = new TbItemParam();
        itemParam.setItemCatId(itemCatId);
        itemParam.setParamData(paramData);
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        int selective = tbItemParamMapper.insertSelective(itemParam);
        return selective;
    }

    @Override
    public int deleteItemParamById(Long id) {
        return  tbItemParamMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbItemParamItem selectTbItemParamItemByItemId(Long itemId) {
//        先查询redis中是否有数据，没有就去数据库查询
        TbItemParamItem tbItemParamItem = (TbItemParamItem) redisClient.get(ITEM_INFO+":"+itemId.toString()+":"+PARAM);
        if (tbItemParamItem!=null){
            return tbItemParamItem;
        }

//        查询数据
        TbItemParamItemExample example = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> itemParamItemList = tbItemParamItemMapper.selectByExampleWithBLOBs(example);
        if (itemParamItemList.size()>0){
            tbItemParamItem = itemParamItemList.get(0);
            if (redisClient.setnx(ITEM_INFO+":"+itemId.toString()+":"+PARAM+":NX","lock",30)) {
                if (tbItemParamItem==null){
                    //        添加到redis缓存
                    redisClient.set(ITEM_INFO+":"+itemId.toString()+":"+PARAM, new TbItemParamItem(1L,"redis空值"));
                    redisClient.expire(ITEM_INFO+":"+itemId.toString()+":"+PARAM, 30);
                }else {
                    //        添加到redis缓存
                    redisClient.set(ITEM_INFO+":"+itemId.toString()+":"+PARAM, tbItemParamItem);
                    redisClient.expire(ITEM_INFO+":"+itemId.toString()+":"+PARAM, ITEM_INFO_EXPIRE);
                }
                redisClient.del(ITEM_INFO+":"+itemId.toString()+":"+PARAM+":NX");
            }

            return tbItemParamItem;
        }
        return null;
    }
}
